package repository

import (
	"dataaysn/internal/model"

	"gorm.io/gorm"
)

type CollectionRepository interface {
	CreateCollection(config *model.CollectionConfig) error
	GetCollection(id string) (*model.CollectionConfig, error)
	UpdateCollection(config *model.CollectionConfig) error
	DeleteCollection(id string) error
	ListCollections() ([]model.CollectionConfig, error)
	TestConnection(conn *model.DBConnection) error
	TestSSHConnection(config *model.SSHConfig) error
}

type gormCollectionRepository struct {
	db *gorm.DB
}

func NewGormCollectionRepository(db *gorm.DB) CollectionRepository {
	return &gormCollectionRepository{db: db}
}

func (r *gormCollectionRepository) CreateCollection(config *model.CollectionConfig) error {
	return r.db.Create(config).Error
}

func (r *gormCollectionRepository) GetCollection(id string) (*model.CollectionConfig, error) {
	var config model.CollectionConfig
	err := r.db.First(&config, id).Error
	return &config, err
}

func (r *gormCollectionRepository) UpdateCollection(config *model.CollectionConfig) error {
	return r.db.Save(config).Error
}

func (r *gormCollectionRepository) DeleteCollection(id string) error {
	return r.db.Delete(&model.CollectionConfig{}, id).Error
}

func (r *gormCollectionRepository) ListCollections() ([]model.CollectionConfig, error) {
	var configs []model.CollectionConfig
	err := r.db.Find(&configs).Error
	return configs, err
}

func (r *gormCollectionRepository) TestConnection(conn *model.DBConnection) error {
	// 实现数据库连接测试逻辑
	return nil
}

func (r *gormCollectionRepository) TestSSHConnection(config *model.SSHConfig) error {
	// 实现SSH连接测试逻辑
	return nil
}
